<html>
<head><meta charset="utf-8"><title>obtain `Loc` from `Span` · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html">obtain `Loc` from `Span`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245628022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20%60Loc%60%20from%20%60Span%60/near/245628022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html#245628022">(Jul 11 2021 at 21:24)</a>:</h4>
<p>If I have a <code>Span</code>, how can I obtain the <code>Loc</code> of the start of it? Trying to determine the indentation of an item for a suggestion (so it matches standard formatting).</p>



<a name="245628529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20%60Loc%60%20from%20%60Span%60/near/245628529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html#245628529">(Jul 11 2021 at 21:39)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">variant</span><span class="p">.</span><span class="n">span</span><span class="p">.</span><span class="n">lo</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">indentation_width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cx</span><span class="p">.</span><span class="n">source_map</span><span class="p">().</span><span class="n">lookup_source_file</span><span class="p">(</span><span class="n">pos</span><span class="p">).</span><span class="n">lookup_file_pos</span><span class="p">(</span><span class="n">pos</span><span class="p">).</span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">indentation</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">" "</span><span class="p">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">indentation_width</span><span class="p">.</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>Is this a complete hack or am I crazy? It <em>does</em> work, at least for standard formatting (I can think of a case where it would be quite ugly, but only if the input already is).</p>



<a name="245628637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20%60Loc%60%20from%20%60Span%60/near/245628637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html#245628637">(Jul 11 2021 at 21:42)</a>:</h4>
<p>I would not try and match formatting in a suggestion, that's what rustfmt is for. Doing it in the compiler (as you've found) is difficult and prone to error.</p>



<a name="245628655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20%60Loc%60%20from%20%60Span%60/near/245628655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html#245628655">(Jul 11 2021 at 21:43)</a>:</h4>
<p>So a suggestion (in this case <code>#[default] Foo</code>) could have poor formatting? It feels odd to have a machine-output suggestion not have "proper" formatting.</p>



<a name="245628657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20%60Loc%60%20from%20%60Span%60/near/245628657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html#245628657">(Jul 11 2021 at 21:43)</a>:</h4>
<p>This is also half me playing around with diagnostics, so it's not a big deal either way.</p>



<a name="245632492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20%60Loc%60%20from%20%60Span%60/near/245632492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html#245632492">(Jul 11 2021 at 23:19)</a>:</h4>
<p>That seems fine to me personally <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="245632572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20%60Loc%60%20from%20%60Span%60/near/245632572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20.60Loc.60.20from.20.60Span.60.html#245632572">(Jul 11 2021 at 23:21)</a>:</h4>
<p>Alright. Up to the reviewer anyways <span aria-label="stuck out tongue" class="emoji emoji-1f61b" role="img" title="stuck out tongue">:stuck_out_tongue:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>